﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.LookAndFeel;
using TERP.WaterClient.Common;
using TERP.WaterClient.DbServices;
using System.Reflection;
using System.IO;

namespace TERP.WaterClient.Views
{
    public partial class FrmLogin : XtraForm
    {
        private const string MSG_WELCOME = "Welcome!";

        /// <summary>
        /// 
        /// </summary>
        public FrmLogin()
        {
            InitializeComponent();
            this.InitControl();
            this.SetLanguage();
            LoadMessage();
            btnUrl.Visible = false;
            this.Icon = EmbeddedRes.Instance.AppIcon;
        }

        private void SetLanguage(){
            this.btnLogin.Text = "Đăng nhập";
            this.btnCancel.Text = "Thoát";
            this.lblUsername.Text = "Tên đăng nhập";
            this.lblPwd.Text = "Mật khẩu";
            this.Text = "Đăng nhập";
			//~!@#$%^&*() Thay doi khi build deploy
			//this.txtUsername.Text = "";// "sysadmin";
			//this.txtPwd.Text = "";// "123";
			this.txtUsername.Text = "sysadmin";
			this.txtPwd.Text = "123";
		}

        private void InitControl(){
            DefaultLookAndFeel dlaf = new DefaultLookAndFeel();
            dlaf.LookAndFeel.SkinName = LayoutStyle.Instance.GetDefaultStyle();
            this.lblWelcome.Appearance.Font = LayoutStyle.FONT_BOLD;
            //
            SystemConfig.Instance.LoadConfig(SystemConfig.AppClient.DesktopHtc);
        }

        public void LoadFinish(bool success){
            if (success)
            {
                FrmMain frmMain = new FrmMain(this);
                frmMain.Show();
            }else{
                this.Show();
            }
        }
        private void LoadMessage()
        {
            Assembly a = Assembly.GetExecutingAssembly();
            string resMessage = "TERP.WaterClient.Images.error_msg.txt";
            //
            try
            {
                Stream stream = a.GetManifestResourceStream(resMessage);
                ErrorMsgHandler.Instance.LoadMessage(stream);
            }
            catch (Exception) { }
        }
        private void btnLogin_Click(object sender, EventArgs e)
        {
            AuthService authSv = new AuthService();
            string strUsername = txtUsername.Text;
            string strPwd = txtPwd.Text;
            //
            //string strErrMsg = "Error";
            //bool loginSuccess = true;
            //do login
            try {
                string[] result = authSv.Signin(strUsername, strPwd);
                SystemGlobal.Instance.SessionId = result[0];
                SystemGlobal.Instance.Username = result[1];
                //
                string strTIdOrg = authSv.GetTIdOrg();
                SystemGlobal.Instance.TIdOrg = strTIdOrg;
				this.Hide();
				new FrmLoading(this).Show();
            }catch (Exception ex){
				Util.ShowExceptionBox(ex);
                //loginSuccess = false;
                //strErrMsg = ex.Message;
            }
            //
			//if (loginSuccess){
			//    lblWelcome.Text = MSG_WELCOME;
			//    lblWelcome.ForeColor = System.Drawing.Color.Black;
			//    this.Hide();
			//    new FrmLoading(this).Show();
			//}else{
			//    lblWelcome.Text = strErrMsg;
			//    lblWelcome.ForeColor = System.Drawing.Color.Red;
			//}
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnUrl_Click(object sender, EventArgs e)
        {
            new FrmWsUrl().ShowDialog(this);
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {

        }
    }
}
